

cc.Class({
    extends: cc.Component,

    properties: {
        virusPrefabArray: [cc.Prefab],
        dieAnima:cc.Prefab,
        virusPrarent:cc.Node,
    },
    ctor:function(){
        this.virusPoolArray =[];
        this.dieAnimaPool = new cc.NodePool();
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        // let node = cc.instantiate(this.virus[0]);
        // node.parent = gameCtrl.node;
        // node.setPosition(125, 675);
        this.createVirusPool();
        window.virusMake = this;
        this.createVirus(0);

    },

    start() {

    },
    createVirusPool(){
        for(let i =0;i<=this.virusPrefabArray.length;i++){
            this.virusPoolArray.push( new cc.NodePool());
        }
        console.log( this.virusPoolArray);
    },
    createVirus(id){

        let virus = null;
        if (this.virusPoolArray[id].size() > 0) {
            virus = this.virusPoolArray[id].get();
        } else {
            console.log(this.virusPrefabArray[id]);
            virus = cc.instantiate(this.virusPrefabArray[id]);
            virus.id = id;
        }
        virus.parent = this.virusPrarent;
        let js = virus.getComponent('virus'+id);
        js.init();
        // virus.setPosition(cc.v2(160,1700));
        return virus;

    },
    onKillVirus(node){
        let id = node.id;
        this.virusPoolArray[id].put(node);
    },
    createDieAnima(node){
        let dieAnima =null;
        if (this.dieAnimaPool.size() > 0) {
            dieAnima = this.dieAnimaPool.get();
        } else {
            dieAnima = cc.instantiate(this.dieAnima);
        }

        dieAnima.parent = this.virusPrarent;
        dieAnima.color = node.color;
        dieAnima.setPosition(node.getPosition());
        dieAnima.setScale(node.scale*2);

        //动画播放结束后，把病毒死亡节点放入池中
        let js =dieAnima.getComponent(cc.Animation);
        js.playOver = ()=>{
            this.onKillDieAnima(dieAnima);
        };
        js.play('virusDie');
    },
    onKillDieAnima(node){
        console.log('病毒死亡动画节点放回对象池');
        this.dieAnimaPool.put(node);
    },
    // update (dt) {},
});